package com.zc.mychart.chart;

import android.content.Context;
import android.widget.TextView;

import com.github.mikephil.charting.charts.Chart;
import com.github.mikephil.charting.components.MarkerView;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.utils.MPPointF;
import com.zc.mychart.R;
import com.zc.mychart.utils.ArithUtil;
import com.zc.mychart.utils.FormatUtil;

/**
 * Created by zhangchi on 2018/3/7.
 */

public class MarkerViewLeftY extends MarkerView {
    private TextView tv_content;
    private MPPointF mOffset = new MPPointF();

    /**
     * Constructor. Sets up the MarkerView with a custom layout resource.
     *
     * @param context
     * @param layoutResource the layout resource to use for the MarkerView
     */
    public MarkerViewLeftY(Context context, int layoutResource) {
        super(context, layoutResource);
        tv_content = (TextView) findViewById(R.id.tv_content);
    }

    public void setMarkerBg(int resId, int textColor) {
        tv_content.setBackgroundResource(resId);
        tv_content.setTextColor(textColor);
    }
    @Override
    public void refreshContent(Entry e, Highlight highlight) {
        tv_content.setText(FormatUtil.formatDouble2(ArithUtil.mul(e.getY() , MyLineChart.mWeight)));
        super.refreshContent(e, highlight);
    }

    @Override
    public MPPointF getOffsetForDrawingAtPoint(float posX, float posY) {
        Chart chart = getChartView();
        if (chart == null)
            return mOffset;
        float height = getHeight();
        mOffset.x = -posX;
        mOffset.y = -height / 2.0f;
        if (posY + mOffset.y < 0) {
            mOffset.y = -posY;
        } else if (chart != null && posY + height + mOffset.y > chart.getHeight()) {
            mOffset.y = chart.getHeight() - posY - height;
        }
        return mOffset;
    }
}
